/**
 ********************************************************************************************************
 * @file           Nixie.c
 * @author         Dingzhenghao
 * @brief          数码管驱动文件
 * MCU:            CMS8S6990
 ********************************************************************************************************
  */
#include <CMS8S6990.H>
#include "BSP_gpio.h"
#include "Nixie.h"
#include "main.h"

//数码管显示缓存区
uint8_t ucNixieBuf[10]={0,10,10,10,10,10,10,10,10,10};									/* 数码管1-8缓存数组， Buf[0]未使用，索引10为全灭	 */
uint8_t	ucNixieBufForCase9 = 0;																/* 指示灯专用显示Buf，考虑后期亮灭组合，单独写一个Buf */	

//数码管段码表（所有显示动画都在这个数组里）
code const uint8_t NixieTable[]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x27, 0x7F, 0x6F, 0x00, 0x40,
								0x54, 0x73, 0x63, 0x39, 0x7A, 0x9C, 0x77, 0x38, 0x31, 0x3E, 0x5E, 0x50, 0x31, 0x39, 0x3A,
								0x40, 0x5C, 0x76, 0x71};

code const uint8_t IndexTable[] = {2, 3, 4, 1, 5, 5, 6, 6, 7, 7, 8, 8, 9, 10, 10};
								
/**
  * @brief  设置显示缓存区
  * @param  Location 要设置的位置，范围：1~8
  * @param  Number 要设置的数字，范围：段码表索引范围
  * @retval 无
  */
void Nixie_SetBuf(uint8_t Location,Number)
{
	ucNixieBuf[Location]=Number;
}

/**
  * @brief  设置指示灯,指示灯Buf专用（case9），指示灯宏在main.h
* @param  SetMode 要设置的魔石，1为置1，0为置0
  * @param  Number 要设置的数字
  * @retval 无
  */
void Nixie_SetBuf_forcase9(uint8_t SetMode, uint8_t Number)
{
	if(SetMode == 0)
	{
		ucNixieBufForCase9 &= Number;
	}
	else if(SetMode == 1)
	{
		ucNixieBufForCase9 |= Number;
	}
	
}

/**
  * @brief  段码并行输出
  * @param  要显示的数字，范围0 ~ 9
  * @retval 无
  */
void Nixie_GPIO_Out(Nixie_Position_t Nixie_Position,uint8_t Number)
{
//	SEG_Enable();
	if(Nixie_Position == PV)
	{
		SEG1 = (~NixieTable[Number] >> 0) & 0x01;
		SEG2 = (~NixieTable[Number] >> 1) & 0x01;
		SEG3 = (~NixieTable[Number] >> 2) & 0x01;
		SEG4 = (~NixieTable[Number] >> 3) & 0x01;
		SEG5 = (~NixieTable[Number] >> 4) & 0x01;
		SEG6 = (~NixieTable[Number] >> 5) & 0x01;
		SEG7 = (~NixieTable[Number] >> 6) & 0x01;
	}
	else if(Nixie_Position == SV)
	{
		SEG1 = (NixieTable[Number] >> 0) & 0x01;
		SEG2 = (NixieTable[Number] >> 1) & 0x01;
		SEG3 = (NixieTable[Number] >> 2) & 0x01;
		SEG4 = (NixieTable[Number] >> 3) & 0x01;
		SEG5 = (NixieTable[Number] >> 4) & 0x01;
		SEG6 = (NixieTable[Number] >> 5) & 0x01;
		SEG7 = (NixieTable[Number] >> 6) & 0x01;
	}
	
}

/**
  * @brief  段码并行输出(case:9专用)
  * @param  要显示的数字
  * @retval 无
  */
void Nixie_GPIO_Out_forcase9(uint8_t Number)
{
//	SEG_Enable();
	SEG1 = (~Number >> 0) & 0x01;
	SEG2 = (~Number >> 1) & 0x01;
	SEG3 = (~Number >> 2) & 0x01;
	SEG4 = (~Number >> 3) & 0x01;
	SEG5 = (~Number >> 4) & 0x01;
	SEG6 = (~Number >> 5) & 0x01;
	SEG7 = (~Number >> 6) & 0x01;
	SEG8 = (~Number >> 7) & 0x01;
}

/**
  * @brief  消影函数
  * @param  无
  * @retval 无
  */
void Nixie_Remove_Shadow(void)
{
//	Delay100us();
	GPIO_Disable(1);
	GPIO_Disable(2);
	GPIO_Disable(3);
	GPIO_Disable(4);
	GPIO_Disable(5);
	

//	SEG_Disable();
//	SEG_Enable();
}


/**
  * @brief  数码管扫描显示
  * @param  Location 要显示的位置，范围：1~8
  * @param  Number 要显示的数字，范围：段码表索引范围
  * @retval 无
  */
void Nixie_Scan(unsigned char Location,Number)
{
	if(Location < 5)
	{
		switch(Location)					//位码输出
		{
			case 1:SEG_Disable();GPIO_Disable(4);/*COM4=1;Nixie_GPIO_Out(PV, 10);Nixie_Remove_Shadow();GPIO_Disable(4);GPIO_Disable(2);GPIO_Disable(3);GPIO_Disable(5);COM1=1;GPIO_Enable(1);*/break;
			case 2:Nixie_GPIO_Out(PV, 10);GPIO_Disable(1);GPIO_Disable(3);GPIO_Disable(4);GPIO_Disable(5);COM2=1;GPIO_Enable(2);break;
			case 3:Nixie_GPIO_Out(PV, 10);GPIO_Disable(2);GPIO_Disable(1);GPIO_Disable(4);GPIO_Disable(5);COM3=1;GPIO_Enable(3);break;
			case 4:Nixie_GPIO_Out(PV, 10);GPIO_Disable(3);GPIO_Disable(1);GPIO_Disable(2);GPIO_Disable(5);COM4=1;GPIO_Enable(4);break;
		}
		Nixie_GPIO_Out(PV, Number);			//段码输出
	}
	else if(Location < 9)
	{
		switch(Location)					//位码输出
		{
			case 5:SEG_Enable();Nixie_GPIO_Out(SV, 10);GPIO_Disable(4);GPIO_Disable(2);GPIO_Disable(3);GPIO_Disable(5);COM1=0;GPIO_Enable(1);break;
			case 6:Nixie_GPIO_Out(SV, 10);GPIO_Disable(1);GPIO_Disable(3);GPIO_Disable(4);GPIO_Disable(5);COM2=0;GPIO_Enable(2);break;
			case 7:Nixie_GPIO_Out(SV, 10);GPIO_Disable(2);GPIO_Disable(1);GPIO_Disable(4);GPIO_Disable(5);COM3=0;GPIO_Enable(3);break;
			case 8:Nixie_GPIO_Out(SV, 10);GPIO_Disable(3);GPIO_Disable(1);GPIO_Disable(2);GPIO_Disable(5);COM4=0;GPIO_Enable(4);break;
		}
		Nixie_GPIO_Out(SV, Number);			//段码输出
	}
	else
	{
		
	}
}

/**
  * @brief  数码管循环扫描函数，在中断中调用（1ms）
  * @param  无
  * @retval 无
  */
void Nixie_Loop(void)
{
	static unsigned char i=2;
	static unsigned char Index = 1;
	Nixie_Scan(i,ucNixieBuf[i]);
	if(i == 9)
	{
		SEG_Disable();GPIO_Disable(4);
	}
	if(i == 10)																	/* case9单独讨论 */
	{
		SEG_Enable();
		Nixie_GPIO_Out_forcase9(0);
		COM5=1;GPIO_Enable(5);GPIO_Disable(4);GPIO_Disable(1);GPIO_Disable(2);GPIO_Disable(3);
		Nixie_GPIO_Out_forcase9(ucNixieBufForCase9);
	}
	i = IndexTable[Index];
	Index++;
	if(Index>=15){Index=0;}
}

